package com.edu.compile.template.ftl.block;


import com.edu.compile.template.AbstractNodeBlockParse;
import com.edu.compile.template.NodeTemplateParse;
import com.edu.compile.template.TemplatePolymerization;
import com.edu.compile.template.buffer.Buffer;
import com.edu.compile.template.ftl.Event;
import com.edu.model.tree.v2.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * http节点模板参数
 *
 * @author yanzhao
 * @date 2021/10/25 17:27
 * @since 1.0.0
 */
public class Http extends AbstractNodeBlockParse implements Event {

    public Http(String nodeId, String nodeType, String nodeName, String parentNodeId) {
        super(nodeId, nodeType, nodeName, parentNodeId);
    }

    /**
     * 需要参数校验不能为null的参数名称集合
     */
    private List<String> paramsCheck = new ArrayList<>();

    /**
     * 为null时需要设置为默认值的集合
     */
    private Map<String, String> defaultValueMap = new HashMap<>();

    public List<String> getParamsCheck() {
        return paramsCheck;
    }

    public void addParamsCheck(String paramsCheck) {
        this.paramsCheck.add(paramsCheck);
    }

    public void putDefaultValueMap(String key, String value) {
        if (key == null || value == null) {
            return;
        }
        defaultValueMap.put(key, value);
    }

    public Map<String, String> getDefaultValueMap() {
        return defaultValueMap;
    }

    /**
     * 解析当前脚本需要调用哪些内容（方法）
     *
     * @param handler
     * @param treeNode
     * @param buffer
     * @return
     */
    @Override
    public String parseMethodCall(TemplatePolymerization handler, TreeNode<NodeTemplateParse> treeNode, Buffer buffer) {
        List<TreeNode<NodeTemplateParse>> childrenNode = treeNode.getChildrenNode();
        Iterator<TreeNode<NodeTemplateParse>> iterator = childrenNode.iterator();
        while (iterator.hasNext()) {
            TreeNode<NodeTemplateParse> next = iterator.next();
            NodeTemplateParse data = next.getData();
            data.parse(handler, next, buffer);
        }

        String s = buffer.toString();
        this.addCall(s);
        return null;
    }
}
